package mo.tags.greedy;

import java.util.Arrays;

public class L910 {

    public int smallestRangeII(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int l = nums[0] + k, r = nums[n-1]-k;
        int ans = nums[n-1] - nums[0];
        for (int i = 1; i < n; i++) {
            int L = nums[i] - k;
            int R = nums[i-1] + k;
            int cur = Math.max(r, R) - Math.min(l, L);
            ans = Math.min(ans, cur);
        }
        return ans;
    }

}
